/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"97F33B12-5EB0-4126-94B7-FDA6CEF777FB",variableType:8}
 */
var fdolar = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"ECF083B5-4FDC-4970-844C-441F6E2F80C5"}
 */
var fLeyenda = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"7E5B5291-C349-46FA-A6FB-AA0AF8A27D30"}
 */
var fmoneda = 'null';

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"2B5E4762-C9D6-428B-8DD9-514810023D2A",variableType:8}
 */
var ftotal = null;

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"C9455032-E07B-46DF-8369-3AAD69FA970A"}
 */
function onShow(firstShow, event) {
	databaseManager.setAutoSave(false)
	if (moneda_carga == 1)
		fmoneda = "$"
	else
		fmoneda = "u$s"
			
	globals.gtotal_debe = 0
	globals.gtotal_haber = 0
	
	for (var i = 1 ; i <= limbo_cab_to_limbo.getSize() ; i++)
	{
		limbo_cab_to_limbo.setSelectedIndex(i)
		
		limbo_cab_to_limbo.importe_carga_debe = limbo_cab_to_limbo.importe_debe
		limbo_cab_to_limbo.importe_carga_haber = limbo_cab_to_limbo.importe_haber
		globals.gtotal_debe += limbo_cab_to_limbo.importe_debe
		globals.gtotal_haber += limbo_cab_to_limbo.importe_haber
	}
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"3F554F80-859B-4E55-85DD-F3C2D3CE78DE"}
 */
function onAction_nuevo_item(event) {
	limbo_cab_to_limbo.newRecord()
	forms.limbo_modif_det.elements.cuentas_id.requestFocus()
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"D438FECF-E35E-4DAB-9586-C8B8E2F306D4"}
 */
function onAction_confirmar(event) {
	var res = globals.DIALOGS.showQuestionDialog('','Actualiza el asiento?','SI','NO')
	if (res == 'NO')
    {
       return 
    } 
	var error = 0
	
	for (var i = 1 ; i <= forms.limbo_modif_det.controller.getMaxRecordIndex() ; i++)
	{
		forms.limbo_modif_det.controller.setSelectedIndex(i)
		
		if (forms.limbo_modif_det.cuentas_id == null || forms.limbo_modif_det.division == null || forms.limbo_modif_det.centro == null)
		{
			error = 1
			globals.DIALOGS.showWarningDialog("Aviso","Existen campos sin completar","Ok")					
			break
		}
			
		if (forms.limbo_modif_det.importe_carga_debe != null && forms.limbo_modif_det.importe_carga_haber != null)
		{
			error = 1
			globals.DIALOGS.showWarningDialog("Aviso","Debe y Haber con importes","Ok")
			break
		}
	}
		
	if (utils.numberFormat(globals.gtotal_debe,2) != utils.numberFormat(globals.gtotal_haber,2))
	{
		error = 1
		globals.DIALOGS.showWarningDialog("Aviso","El Total del Debe no coincide con el Total del Haber","Ok")
	}
	
	if (error == 1)			
		globals.DIALOGS.showWarningDialog("Aviso","Asiento sin actualizar","Ok")			
	else
	{
		if (globals.gtotal_debe != total)
		{
			var res = globals.DIALOGS.showQuestionDialog('','Total diferente, continua?','SI','NO')			    
			if (res == 'NO')
			{
				return
			}
		}
		// recorre y da de baja los de importe = 0
		var i = 1
		while (i <= forms.limbo_modif_det.controller.getMaxRecordIndex())
		{
			forms.limbo_modif_det.controller.setSelectedIndex(i)
			if (forms.limbo_modif_det.importe_carga_debe == null || forms.limbo_modif_det.importe_carga_debe == 0)
			{
				var imp = forms.limbo_modif_det.importe_carga_haber.toFixed(2)*1
				forms.limbo_modif_det.columna = 'H'
			}
			else
			{
				var imp = forms.limbo_modif_det.importe_carga_debe.toFixed(2)*1	
				forms.limbo_modif_det.columna = 'D'
			}
			if (moneda_carga == 1)
				forms.limbo_modif_det.importe = imp
			else
				forms.limbo_modif_det.impodol = imp
			
			
			if ((forms.limbo_modif_det.importe_carga_debe == 0 || forms.limbo_modif_det.importe_carga_debe == null) && 
			(forms.limbo_modif_det.importe_carga_haber == 0 || forms.limbo_modif_det.importe_carga_haber == null))
			{
				forms.limbo_modif_det.controller.deleteRecord()
			}
			else
			{
				i++	
			}
		}
		total = globals.gtotal_debe
		fConversionDolares()
	    moneda = "A"
		if (moneda_carga == 1)
		{
			moneda = "P"
		}
		else
		{
			moneda = "D"
		}
		
		databaseManager.startTransaction()
		var estado = databaseManager.saveData()
		if (estado == false){
			databaseManager.rollbackTransaction()
			databaseManager.revertEditedRecords()
			globals.DIALOGS.showWarningDialog("Aviso","No se actualizaron los datos","Ok")
			return
		}
		databaseManager.commitTransaction()
		databaseManager.setAutoSave(true)
        var $win = application.getWindow('LIMBO')
		$win.hide()
		$win.destroy()
	}
}

/**
 * @properties={typeid:24,uuid:"51B7ED32-6810-40D0-817B-D1D2AB5C85F4"}
 */
function fConversionDolares()
{
	var vTotalD = 0
	var vTotalH = 0
	var $cant = databaseManager.getFoundSetCount(forms.limbo_modif_det.foundset)
	for (var i = 1 ; i <= $cant ; i++)
	{
		var $rcd = forms.limbo_modif_det.foundset.getRecord(i)
		
		if (moneda_carga == 1)//Pesos
		{
			var impodol = $rcd.importe / dolar
			if (impodol != null)
				$rcd.impodol = impodol.toFixed(2)*1
			
			if ($rcd.columna == 'D')
				vTotalD += $rcd.impodol
			else
				vTotalH += $rcd.impodol 
		}
		else
		{
			var importe = $rcd.impodol * dolar
			if (importe != null)
				$rcd.importe = importe.toFixed(2)*1
				
			if ($rcd.columna == 'D')
				vTotalD += $rcd.importe
			else
				vTotalH += $rcd.importe
		}
	}
	
	if (vTotalD != vTotalH)
	{
		var dif = vTotalD - vTotalH
		if ($rcd.columna == 'H')
		{	
			if (moneda_carga == 1)//Pesos
				$rcd.impodol += dif
			else
				$rcd.importe += dif
		}
		else
		{
			if (moneda_carga == 1)//Pesos
				$rcd.impodol -= dif
			else
				$rcd.importe -= dif
		}
	}
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"3C6F9372-5EDB-4DFC-A082-C6F40201E93A"}
 */
function onAction_cancelar(event) {
	databaseManager.revertEditedRecords()
	databaseManager.setAutoSave(true)
	var $win = application.getWindow('LIMBO')
	$win.hide()
	$win.destroy()
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"7B10AF41-7873-4DA2-AE01-D69BFFD45B80"}
 */
function onAction_ordenar(event) {
	forms.limbo_modif_det.controller.sort('columna asc, cuentas_id asc')
}

/**
 * Handle changed data.
 *
 * @param {Object} oldValue old value
 * @param {Object} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"C5122113-2D72-4A73-A2C8-851CFF2FB203"}
 */
function onDataChange_fecha(oldValue, newValue, event) {
	fdolar = globals.gBuscarCotizacion(fecha)
	return true
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"BCFD58F6-1477-400C-828A-BC429554C63A"}
 */
function onAction_eliminar(event) {
	var res = globals.DIALOGS.showQuestionDialog('','Elimina el asiento?','SI','NO')
	if (res == 'NO')
    {
       return 
    } 	
	databaseManager.startTransaction()
	limbo_cab_to_limbo.deleteAllRecords()
	controller.deleteRecord()
	if (!databaseManager.commitTransaction())
    {
     	databaseManager.rollbackTransaction()
    } 
	databaseManager.setAutoSave(true)	
	var $win = application.getWindow('LIMBO')
	$win.hide()
	$win.destroy()
}
